<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\BabyService;
use Session;

class BabyController extends Controller{
	public function insert(){

        date_default_timezone_set('PRC');
        $baby = new BabyService;
        $data['baby_nick'] = addslashes($_POST['baby_nick']);
        $data['baby_sex'] = intval($_POST['sex']);
        $data['baby_birthday'] = addslashes($_POST['birthday']);
        // $data['baby_age'] = intval(date('Y'))-intval(substr($_POST['baby_birthday']),0,4);
        $data['baby_age'] = intval($_POST['age']);
        $data['member_id'] = intval($_POST['member_id']);
        $babyinfo = $baby->getBabyInfo(intval($_POST['member_id']),$data['baby_nick']);
        if(!empty($babyinfo)){
            $_data = array('code'=>'3x000002','msg'=>'宝宝信息已存在,请勿重复添加');
        }else{
            $res = $baby->insertBaby($data);
            if($res){
                $_data = array('code'=>'2x000001','msg'=>'宝宝信息添加成功');
            }else{
                $_data = array('code'=>'4x000001','msg'=>'宝宝信息添加失败');
            }
        }

		
		return response()->json($_data);
	}

	public function list($mid){
        if(!$mid){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $baby = new BabyService;
            $list = $baby->getBabyList($mid);
            if($list !== false){
                $_data = array('code'=>'2x000001','list'=>$list);
            }else{
                $_data = array('code'=>'4x000001','msg'=>'暂无宝宝信息，请设置');
            }
        }
		return response()->json($_data);
	}

	public function getBabyInfo($id){

        $baby = new babyService;
        $babyinfo = $baby->getBabyInfo($id);
        if($babyinfo !== false){
            $_data = array('code'=>'2x000001','babyinfo'=>$babyinfo);
        }else{
            $_data = array('code'=>'4x000001','msg'=>'暂无该宝宝信息');
        }

		return response()->json($_data);
	}

	public function updateBaby(){

        $baby = new BabyService;
        $id = intval($_POST['id']);

        $data['baby_nick'] = addslashes($_POST['baby_nick']);
        $data['baby_sex'] = intval($_POST['sex']);
        $data['baby_birthday'] = addslashes($_POST['birthday']);
        $data['baby_age'] = intval($_POST['age']);

        $res = $baby->modifyBaby($id,$data);

        if($res){
            $_data = array('code'=>'2x000001','msg'=>'宝宝信息更新成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'宝宝信息更新失败');
        }

		return response()->json($_data);
	}

	public function del($id){
		$baby = new BabyService;
		$res = $baby->delBaby($id);
		if($res){
			$_data = array('code'=>'2x000001','msg'=>'宝宝信息删除成功');
		}else{
			$_data = array('code'=>'4x000001','msg'=>'宝宝信息删除失败');
		}

		return response()->json($_data);
	}
}